import Vue from 'vue'
import Router from 'vue-router'
import { getToken } from './libs/http';


Vue.use(Router);

const router = new Router({
    routes: [
        {
            path: '/index',
            name: 'index',
            meta: {title: '店铺主页'},
            component: () => import('./views/store_index/index.vue')
            // component:resolve => require(['./views/store_index/index.vue'], resolve),
        },


        // {path:'/', redirect:'/index'},

        {
            path: '/login',
            name: 'login',
            meta: {title: '登录'},
            component:()=> import('./views/login')
        },

        {
            path: '/custom',
            name: 'custom',
            meta: {title: '客服'},
            component: () => import('./views/custom_message/index.vue')

        },

        {
            path: '/shop_car',
            name: 'shop_car',
            meta: {title: '购物车'},
            component: () => import('./views/shop_car/index.vue')
        },

        {
            path: '/shop_info',
            name: 'shop_info',
            meta: {title: '商品详情'},
            component:resolve => require(['./views/shop_info/index.vue'], resolve),

            // component: () => import('./views/shop_info/index.vue')
        },

        {
            path: '/select_area',
            name: 'select_area',
            meta: {title: '填写信息'},
            component: () => import('./views/select_area/index.vue')
        },

        {
            path: '/app_store',
            name: 'app_store',
            meta: {title: '登录'},
            component:()=> import('./views/app_store_index')
        },

        {
            path: '/task_info',
            name: 'task_info',
            meta: {title: '详情'},
            component:()=> import('./views/task_info/index.vue')
        },

        {
            path: '/good_info',
            name: 'good_info',
            meta: {title: '商品详情'},
            component:()=> import('./views/task_info/goods.vue')
        },
    ],
});
router.beforeEach((to, from, next) => {
    const LOGIN_PAGE_NAME = 'login';
    const token = getToken();
    function isWeiXin() {
        let ua = window.navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == "micromessenger") {
            return true;
        }
        else {
            return false;
        }
    }
    function GetUrlParam(name){
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
        var r = window.location.hash.substr(1).match(reg)
        if(r != null){
            return unescape(r[2])
        }else{
            return null

        }
    }
    if(!isWeiXin()){
        if(to.name == "app_store" || (to.name == "task_info") || (to.name == "good_info")){
            next();
        }else{
            if (!token && to.name !== LOGIN_PAGE_NAME) {
                console.log(window)
                sessionStorage.setItem('task_order_id',GetUrlParam('task_order_id'))
                next({ name:LOGIN_PAGE_NAME, query:{name:'shop_info', id: window.location.hash.split('?')[1].split('&')[0].split('=')[1]} });
            } else {
                next();
            }
        }
    }else {
        next();
       /* const code = GetUrlParam('code') // 截取路径中的code，如果没有就去微信授权，如果已经获取到了就直接传code给后台获取openId
        const local = window.location.href
        if (code == null || code === '') {
            window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + 'wx504b75c5ff0b0b7f' + '&redirect_uri=' + encodeURIComponent(local) + '&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect'
        } else {
            sessionStorage.setItem('code',code) //把code传给后台获取用户信息
            next();
        }
        console.log(sessionStorage.getItem('code'))*/
    }

});

router.afterEach((to,from) => {
    window.scrollTo(0,0);
});


export default router;
